import Vue from "vue";

//配置路由相关信息
import VueRouter from "vue-router";

import store from "../store/index";

import CompanyList from "../views/admin/CompanyList";
import UniversityList from "../views/admin/UniversityList";
import ComplaintApp from "../views/admin/ComplaintApp";
import CompanyApp from "../views/admin/CompanyApp";
import CompanyDetails from "../views/admin/CompanyDetails";
import UniversityApp from "../views/admin/universityApp";
import PwdChange from "../views/admin/PwdChange";
import adminLogin from "../views/admin/Login";
import LogoutUser from "../views/admin/LogoutUser";
import UniversityDetails from "../views/admin/UniversityDetails";
import PracticeBank from "../views/admin/PracticeBank.vue";
import StudyVideoList from "../views/admin/StudyVideoList.vue";

// 页面刷新时，重新赋值token
if (sessionStorage.getItem("token")) {
  store.commit("set_token", sessionStorage.getItem("token"));
}

//1.通过Vue.use(插件),安装插件
Vue.use(VueRouter);

//2.创建路由对象
const routes = [
  {
    path: "",
    redirect: "/login",
  },
  {
    path:'/loading',
    component:()=>import("../components/common/Loading")

  },
  {
    path:'/upload',
    component: ()=>import("../views/admin/upload")
  },
  {
    path:'/upload2',
    component:()=>import("../views/admin/upload2")
  },
  {
    path:"/admin",
    component:()=>import("../views/admin/Admin"),
    meta: {
      title: "首页"
    },
    children: [
      {
        path: '',
        redirect: 'dashBoard'
      },
      {
        path:'dynamicReDetails',
        component:()=>import("../views/admin/dynamicReDetails"),
        meta: {
          title: '动态举报详情',
          requireAuth: true
        },
      },
      {
        path:'recReDetails',
        component:()=>import("../views/admin/recReDetails"),
        meta: {
          title: '招聘举报详情',
          requireAuth: true
        },
      },
      {
        path: "dashBoard",
        component: () => import("../views/admin/Dashboard.vue"),
        meta: {
          title: '统计',
          requireAuth: true
        },
      },
      {
        path: "companyList",
        component: CompanyList,
        meta: {
          title: "企业列表",
          requireAuth: true,
        },
      },
      {
        path: "universityList",
        component: UniversityList,
        meta: {
          title: "学校列表",
          requireAuth: true,
        },
      },
      {
        path:'studentList',
        component:()=>import("../views/admin/StudentList"),
        meta:{
          title: "学生列表",
          requireAuth: true
        }
      },
      {
        path: "auditCompany",
        component: CompanyApp,
        name: "companyApp",
        meta: {
          title: "企业驻入申请",
          requireAuth: true,
        },
      },
      {
        path: "auditUniversity",
        component: UniversityApp,
        meta: {
          title: "学校驻入申请",
          requireAuth: true,
        },
      },
      {
        path: "auditComplaint",
        component: ComplaintApp,
        meta: {
          title: "投诉列表",
          requireAuth: true
        },
      },
      {
        path:'videoApp',
        component:()=>import("../views/admin/VideoApp"),
        meta: {
          title: '视频提交审核',
          requireAuth: true
        }
      },
      {
        path: "studyVideo",
        component: StudyVideoList,
        meta: {
          title: "视频管理",
          requireAuth: true
        },
      },
      {
        path: "practice",
        component: PracticeBank,
        meta: {
          title: "题库",
          requireAuth: true
        },
      },
      {
        path:'companyPractice',
        component:()=>import("../views/admin/companyPractice"),
        meta: {
          title: "企业题库",
          requireAuth: true
        }
      },
      {
        path:'companyRecList',
        component:()=>import("../views/admin/recList"),
        meta:{
          title: "岗位管理",
          requireAuth: true
        }
      },
      {
        path: 'companyStudyVideo',
        component:()=>import("../views/admin/companyVideo"),
        meta: {
          title: "企业视频",
          requireAuth: true
        }
      },

      {
        path: "pwdChange",
        component: PwdChange,
        meta: {
          title: "修改密码",
          requireAuth: true
        },
      },
      {
        path: 'uploadVideo',
        component:()=>import("../views/admin/uploadVideo")
      },  {
        path: "details/:id",
        name: "details",
        component: CompanyDetails,
        meta: {
          title: "企业申请详情",
          requireAuth: true
        },
      },
      {
        path: "detailsU/:id",
        name:'details',
        component: UniversityDetails,
        meta: {
          title: "高校申请详情",
          requireAuth: true
        }
      },
      {
        path:'reportType',
        name: 'reportType',
        component:()=>import("../views/admin/reportType"),
        meta: {
          title: "举报类型管理",
          requireAuth: true
        }
      }
    ]
  },



  {
    path: "logoutUser",
    component: LogoutUser,
    meta: {
      title: "注销用户",
    },
  },
  {
    path: "/login",
    component: adminLogin,
    meta: {
      title: "后台登陆",
    },
  },
];

const router = new VueRouter({
  //配置路由和组件之间的关系
  routes,
  mode: "history",
  linkActiveClass: "active",
});

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.requireAuth)) {
    //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
    if (store.getters.token) {
      next();
      document.title = to.meta.title;
      let matched=to.matched
      console.log(to.matched);
      let list=[];
      matched.forEach(element => {
        list.push({"title":element.meta.title,"path": element.path})
      });
      store.dispatch("user/setBreads",list)
    } else {
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
});



//后置钩子(hook)
router.afterEach((route) => {
  console.log(route.fullPath)
});

//解决两次访问相同路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 3.将router对象传到vue实例
export default router;

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
